package com.cqc.manage.util.thrift;

import lombok.Data;
import lombok.Setter;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.springframework.stereotype.Component;

@Data
public class ThriftClient {
    private String host;
    private Integer port;

    private TTransport tTransport;

    private TBinaryProtocol tProtocol;

    private getBusiness.Client client;

    private void init() {
        tTransport = new TSocket(host, port);
        //协议对象 这里使用协议对象需要和服务器的一致
        tProtocol = new TBinaryProtocol(tTransport);
        client = new getBusiness.Client(tProtocol);
    }

    public getBusiness.Client getService() {
        return client;
    }

    public void open() throws TTransportException {
        if (null != tTransport && !tTransport.isOpen())
            tTransport.open();
    }

    public void close() {
        if (null != tTransport && tTransport.isOpen())
            tTransport.close();
    }

}
